//
//  ModifyPwdViewController.m
//  shuodi
//
//  Created by 孔晓寅 on 15/12/5.
//  Copyright © 2015年 Suzhou fish Information Technology Co., Ltd. All rights reserved.
//

#import "ModifyPwdViewController.h"

@interface ModifyPwdViewController (){
    NSString *pwdflg;
}

@end

@implementation ModifyPwdViewController

- (instancetype)initWithInfo:(NSObject *)obj{
    self = [super init];
    if (self) {
        pwdflg = (NSString*)obj;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setCustomRightText:self action:@selector(loginclick:) text:@"保存"];
    [self initControl];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loginclick:(id)sender{
    [self.view endEditing:YES];
    [self loginRequest];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void) initControl {
    [self.view setBackgroundColor:[utils getBackGroupColorGray]];
    CGFloat bottom = [utils getViewBottom:self.navigationController.navigationBar]+OFFSET_TWN;
    
    if ([pwdflg isEqualToString:@"0"]) {
        
        self.title = @"密码修改";
        
        _passwdEdit = [[UITextField alloc] init];
        UIView *passWord = [self setEdit:@"icon_yanzhengma" withview:_passwdEdit withtext:@"" withplaceholder:@"请输入原始密码" withY:bottom];
        _passwdEdit.text = @"";
        _passwdEdit.secureTextEntry = YES;
        [self.view addSubview:passWord];
        bottom = [utils getViewBottom:passWord]+0.5 ;
        
        _newpasswdEdit = [[UITextField alloc] init];
        UIView *newpassWord = [self setEdit:@"icon_password" withview:_newpasswdEdit withtext:@"" withplaceholder:@"请输入新密码" withY:bottom];
        _newpasswdEdit.text = @"";
        _newpasswdEdit.secureTextEntry = YES;
        [self.view addSubview:newpassWord];
        
        bottom = [utils getViewBottom:newpassWord]+0.5 ;
        
        _newconfirmpasswdEdit= [[UITextField alloc] init];
        UIView *newconfirmpasswd = [self setEdit:@"icon_password" withview:_newconfirmpasswdEdit withtext:@"" withplaceholder:@"请再次确认新密码" withY:bottom];
        _newconfirmpasswdEdit.text = @"";
        _newconfirmpasswdEdit.secureTextEntry = YES;
        [self.view addSubview:newconfirmpasswd];
        
        bottom = [utils getViewBottom:newconfirmpasswd]+0.5 ;

    }else{
        
        self.title = @"设置密码";
        
        _newpasswdEdit = [[UITextField alloc] init];
        UIView *newpassWord = [self setEdit:@"icon_password" withview:_newpasswdEdit withtext:@"" withplaceholder:@"请设置密码" withY:bottom];
        _newpasswdEdit.text = @"";
        _newpasswdEdit.secureTextEntry = YES;
        [self.view addSubview:newpassWord];
        
        bottom = [utils getViewBottom:newpassWord]+0.5 ;
        
        _newconfirmpasswdEdit= [[UITextField alloc] init];
        UIView *newconfirmpasswd = [self setEdit:@"icon_password" withview:_newconfirmpasswdEdit withtext:@"" withplaceholder:@"请再次输入密码" withY:bottom];
        _newconfirmpasswdEdit.text = @"";
        _newconfirmpasswdEdit.secureTextEntry = YES;
        [self.view addSubview:newconfirmpasswd];
        
        bottom = [utils getViewBottom:newconfirmpasswd]+0.5 ;
        
    }
    
    
//    _change_button = [utils getButton:CGRectMake(OFFSET_TWN, bottom, self.view.frame.size.width-OFFSET_TWN*2, _passwdEdit.frame.size.height) withName:NSLocalizedString(@"OK", nil)];
//    [self.view addSubview:_change_button];
//    [_change_button addTarget:self action:@selector(loginclick:) forControlEvents: UIControlEventTouchUpInside];
    
}

-(UIView *)setEdit:(NSString *)img_url withview:(UITextField*)subview withtext:(NSString*)text withplaceholder:(NSString*)placeholder withY:(CGFloat)point_y with_w:(CGFloat)width{
    
    CGFloat left_x = 0;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(left_x, point_y, width-left_x*2, 44)];
    [view setBackgroundColor:[UIColor whiteColor]];
    
//    UIImage *img = [UIImage imageNamed:img_url];
//    CGFloat icon_img_h = view.frame.size.height - 28;
//    CGFloat icon_img_w = img.size.width/img.size.height*icon_img_h;
//    UIImageView *icon_img = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, icon_img_w, icon_img_h)];
//    [icon_img setImage:img];
    
    [subview setFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    subview.text = text;
    subview.textColor = [UIColor blackColor];
    subview.font = [UIFont systemFontOfSize:15];
    subview.placeholder = placeholder;
    [subview.layer setCornerRadius:5];
    [subview.layer setBorderWidth:0];
    [subview.layer setBorderColor:[[utils getBorderColorGray] CGColor]];
    [subview setBackgroundColor:[UIColor whiteColor]];
    subview.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 0)];
    subview.leftViewMode = UITextFieldViewModeAlways;
    subview.delegate = self;
    //    [subview addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
    subview.returnKeyType = UIReturnKeyDone;
    
    [view addSubview:subview];
//    [view addSubview:icon_img];
    
    return view;
    
}

-(UIView *)setEdit:(NSString *)img_url withview:(UITextField*)subview withtext:(NSString*)text withplaceholder:(NSString*)placeholder withY:(CGFloat)point_y{
    
    return [self setEdit:img_url withview:subview withtext:text withplaceholder:placeholder withY:point_y with_w:self.view.frame.size.width];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    CGRect frame = [textField superview].frame;
    //    CGFloat offset_y = self.myScrollView.contentOffset.y;
    
    //在这里我多加了62，（加上了输入中文选择文字的view高度）这个依据自己需求而定
    int offset = (frame.origin.y+120)-(self.view.frame.size.height-216.0);//键盘高度216
    
    if (offset>0) {
        
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        
        [UIView setAnimationDuration:0.30f];//动画持续时间
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    //输入框编辑完成以后，当键盘即将消失时，将视图恢复到原始状态
    self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
}

-(void)loginRequest{
    
    if ([pwdflg isEqualToString:@"0"]) {
        
        if ([_passwdEdit.text isEqualToString:@""]) {
            [OMGToast showWithText:@"请输入原始密码" bottomOffset:50 duration:1];
            return;
        }
        
        if ([_newpasswdEdit.text isEqualToString:@""]) {
            [OMGToast showWithText:@"请输入新密码" bottomOffset:50 duration:1];
            return;
        }
        if ([_newconfirmpasswdEdit.text isEqualToString:@""]) {
            [OMGToast showWithText:@"请再次确认新密码" bottomOffset:50 duration:1];
            return;
        }
       
    
    }else{
        
        
        if ([_newpasswdEdit.text isEqualToString:@""] && [_newconfirmpasswdEdit.text isEqualToString:@""]) {
            [OMGToast showWithText:@"请设置密码" bottomOffset:50 duration:1];
            return;
        }
        
        if (![_newpasswdEdit.text isEqualToString:@""] && [_newconfirmpasswdEdit.text isEqualToString:@""]) {
            [OMGToast showWithText:@"请再次输入密码" bottomOffset:50 duration:1];
            return;
        }
        
        if ([_newpasswdEdit.text isEqualToString:@""] && ![_newconfirmpasswdEdit.text isEqualToString:@""]) {
            [OMGToast showWithText:@"请设置密码" bottomOffset:50 duration:1];
            return;
        }
        
      
        
    }
    
    if ([_newconfirmpasswdEdit.text isEqualToString:_newpasswdEdit.text]) {
    }else{
        [OMGToast showWithText:@"两次输入密码不一致，请重新输入" bottomOffset:50 duration:1];
        return;
    }
  
    
    NSDictionary *dic = @{@"password":_passwdEdit.text?_passwdEdit.text:@"",@"newPassword":_newpasswdEdit.text};
    __weak typeof(self) weakSelf = self;
    [HttpNetWorkUtil requestupdatePassword:dic  success:^(id json) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        if (json) {
//            int code = (int)[[json objectForKey:@"code"]integerValue];
            
            InfoDataModel *personInfoModel = [utils getPersonInfo];
            
            personInfoModel.infomodel.m.pwdFlag = @"0";
            NSData *person = [RFJModel toDataWithModel:personInfoModel];
            [utils setObject:person forkey:PERSONINFO];
            
            [OMGToast showWithText:@"密码修改成功" bottomOffset:50 duration:1];
            [strongSelf.navigationController popViewControllerAnimated:YES];
        }
        
    } fail:^{}];
}

@end
